package estates.model;

import estates.util.Identifiable;
import javax.persistence.*;
import java.io.Serializable;
import java.util.HashMap;

public class Comment extends Identifiable implements Serializable, Comparable<Comment> {

  private Long id;
  
  private String title;
  private String content;
  private boolean accepted;
  private boolean deleted;
  private String cause;
  private int ocenaEstetyka;
  private int ocenaKomfort;
  private int ocenaLokalizacja;

  public Comment() {
	  
  }

  public static Comment createComment(HashMap<String, Object> commentData, User user, Estate estate) {
	  Comment comm = new Comment();
	  comm.title = (String)commentData.get("title");
	  comm.content = (String)commentData.get("content");
	  comm.ocenaKomfort = (commentData.containsKey("ocenaKomfort")) ? Integer.parseInt(commentData.get("ocenaKomfort").toString()) : 5;
	  comm.ocenaLokalizacja = (commentData.containsKey("ocenaLokalizacja")) ? Integer.parseInt(commentData.get("ocenaLokalizacja").toString()) : 5;
	  comm.ocenaEstetyka = (commentData.containsKey("ocenaEstetyka")) ? Integer.parseInt(commentData.get("ocenaEstetyka").toString()) : 5;
	  comm.accepted = false;
	  comm.deleted = false;
	  comm.cause = null;
	  comm.estate = estate;
	  comm.user = user;
	  return comm;
  }

  public int getOcenaEstetyka()
  {
	  return ocenaEstetyka;
  }
  
  public int getOcenaKomfort()
  {
	  return ocenaKomfort;
  }
  
  public int getOcenaLokalizacja()
  {
	  return ocenaLokalizacja;
  }
  
  public void setOcenaEstetyka(int val)
  {
	  ocenaEstetyka = val;
  }
  
  public void setOcenaKomfort(int val)
  {
	  ocenaKomfort = val;
  }
  
  public void setOcenaLokalizacja(int val)
  {
	  ocenaLokalizacja = val;
  }


  private User user;

  private Estate estate;

  @Override
  public Long getId() {
    return id;
  }

  public User getUser() {
    return user;
  }

  public void setUser(User user) {
    this.user = user;
  }

  public Estate getEstate() {
    return estate;
  }

  public void setEstate(Estate estate) {
    this.estate = estate;
  }

  @Override
  public int compareTo(Comment c) {
    return content.compareTo(c.content);
  }

  @Override
  public boolean equals(Object cmp) {
    if (cmp instanceof Comment) {
      Comment cmn = (Comment) cmp;
      if (!this.id.equals(cmn.id)) {
        return false;
      }
      if (!this.title.equals(cmn.title)) {
        return false;
      }
      if (!this.content.equals(cmn.content)) {
        return false;
      }
      if (!this.user.equals(cmn.user)) {
        return false;
      }
      if (!this.estate.equals(cmn.estate)) {
        return false;
      }
      if (!this.content.equals(cmn.content)) {
        return false;
      }
      return true;
    } else {
      return false;
    }
  }

  @Override
  public int hashCode() {
      int hash = 7;
      hash = 31 * hash + user.getNIP().hashCode();
      hash = 31 * hash + estate.getNumer_oferty();
      hash = 31 * hash + title.hashCode();
      hash = 31 * hash + content.hashCode();
      return hash;
  }

  @Override
  public String toString() {
    StringBuilder sb = new StringBuilder();
    sb.append("id=" + id.toString() + '&');
    sb.append("tytul=" + title + '&');
    sb.append("tresc=" + content + '&');
    sb.append("uzytkownik=" + user.getNIP() + '&');
    sb.append("oferta=" + estate.getNumer_oferty() + '&');
    sb.append("zaakceptowany=" + (accepted ? "tak" : "nie") + '&');
    sb.append("usuniety=" + (deleted ? "tak" : "nie"));
    if (deleted) {
      sb.append("&powod=" + cause);
    }
	sb.append("&estetyka=" + ocenaEstetyka);
	sb.append("&lokalizacja=" + ocenaLokalizacja);
	sb.append("&komfort=" + ocenaKomfort);

    return sb.toString();
  }

  public String getTitle() {
    return title;
  }

  public void setTitle(String title) {
    this.title = title;
  }

  public String getContent() {
    return content;
  }

  public void setContent(String content) {
    this.content = content;
  }

  public boolean isAccepted() {
    return accepted;
  }

  public boolean isDeleted() {
    return deleted;
  }

  public String getCause() {
    return cause;
  }

  public void setDeleted(boolean deleted) {
    this.deleted = deleted;
  }

  public void setAccepted(boolean accepted) {
    this.accepted = accepted;
  }

  public void setCause(String cause) {
    this.cause = cause;
  }
}
